/**
 * @project name:crm
 */
package com.yunbo.crm.service.impl;

import com.yunbo.crm.base.BaseServiceImpl;
import com.yunbo.crm.dao.CusDevPlanMapper;
import com.yunbo.crm.dao.SaleChanceMapper;
import com.yunbo.crm.pojo.CusDevPlan;
import com.yunbo.crm.query.CusDevPlanQuery;
import com.yunbo.crm.service.CusDevPlanService;
import com.yunbo.crm.utils.AssertUtil;
import com.yunbo.crm.utils.CookieUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2021/1/24 18:03
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@Service
public class CusDevPlanServiceImpl extends BaseServiceImpl<CusDevPlan, Integer> implements CusDevPlanService {
    @Resource
    private CusDevPlanMapper cusDevPlanMapper;
    @Resource
    private SaleChanceMapper saleChanceMapper;

    /**
     * 拿到此ID绑定下的所有计划信息
     */
    @Override
    public Map<String, Object> querCusDevPlansByParams(CusDevPlanQuery cusDevPlanQuery) {
        return queryByParamsForTable(cusDevPlanQuery);
    }

    /**
     * 保存新的营销计划
     * 得到创建人
     * 重置更新时间
     * 保存
     *
     * @param cusDevPlan
     * @param request
     */
    @Override
    public void saveCusDevPlan(CusDevPlan cusDevPlan, HttpServletRequest request) {
        String loginUser = getLoginUserName(request);
        //判断 营销机会ID 计划名称，计划内容，计划日期是否为空
        checkParams(cusDevPlan);
        //设置数据有效性
        cusDevPlan.setActiveFlag(1);
        //设置创建人ID
        cusDevPlan.setCreateBy(loginUser);
        cusDevPlan.setCreateDate(new Date());
        cusDevPlan.setUpdateDate(new Date());
        AssertUtil.isTrue(cusDevPlanMapper.insertSelective(cusDevPlan) < 1, "计划项记录添加失败");
    }

    /**
     * 计划项更新
     *
     * @param cusDevPlan
     * @param request
     */
    @Override
    public void updateCusDevPlan(CusDevPlan cusDevPlan, HttpServletRequest request) {
        AssertUtil.isTrue(null == cusDevPlan.getRowId() || null == selectByPrimaryKey(cusDevPlan.getRowId()), "待更新记录不存在");
        checkParams(cusDevPlan);
        cusDevPlan.setUpdateDate(new Date());
        cusDevPlan.setUpdateBy(getLoginUserName(request));
        AssertUtil.isTrue(updateByPrimaryKeySelective(cusDevPlan) < 1, "计划项记录更新失败!");
    }

    /**
     * 删除计划
     *
     * @param cusDevPlan
     * @param request
     */
    @Override
    public void deleteCusDevPlan(CusDevPlan cusDevPlan, HttpServletRequest request) {
        cusDevPlan = selectByPrimaryKey(cusDevPlan.getRowId());
        AssertUtil.isTrue(null == cusDevPlan.getRowId() || null == cusDevPlan, "待删除记录不存在");
        cusDevPlan.setActiveFlag(0);
        AssertUtil.isTrue(updateByPrimaryKeySelective(cusDevPlan) < 1, "计划删除失败");
    }

    private void checkParams(CusDevPlan cusDevPlan) {
        AssertUtil.isTrue(null == cusDevPlan.getSaleChanceId()
                || null == saleChanceMapper.selectByPrimaryKey(cusDevPlan.getSaleChanceId()), "营销机会ID为空");
        AssertUtil.isTrue(null == cusDevPlan.getPlanDate(), "请设定计划项日期");
        AssertUtil.isTrue(StringUtils.isBlank(cusDevPlan.getPlanItem()), "请输入计划项名称");
        AssertUtil.isTrue(null == cusDevPlan.getExeAffect(), "请输入计划项内容");
    }


}
